// @algorithm @lc id=100317 lang=cpp
// @title er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    // @test([5,4,8,11,null,13,4,7,2,null,null,5,1],22)=[[5,4,11,2],[5,8,4,5]]
    // @test([5,4,8,11,null,13,4,7,2,null,null,5,1],22)=[[5,4,11,2],[5,8,4,5]]
    // @test([],0)=[]
    // @test([5,4,8,11,null,13,4,7,2,null,null,5,1],22)=[[5,4,11,2],[5,8,4,5]]
    vector<vector<int>> ans;
    vector<int> path;

public:
    vector<vector<int>> pathSum(TreeNode *root, int target) {
        if (root == nullptr) {
            return ans;
        }
        vector<int> path;
        dfs(root, target);
        return ans;
    }

    void dfs(TreeNode *root, int target) {
        path.push_back(root->val);
        target -= root->val;
        // root是叶子节点
        if (root->left == nullptr && root->right == nullptr) {
            if (target == 0) {
                ans.push_back(path);
                path.pop_back();
                return;
            }
            else {
                path.pop_back();
                return;
            }
        }
        if (root->left) {
            dfs(root->left, target);
        }
        if (root->right) {
            dfs(root->right, target);
        }
        path.pop_back();
    }
};